package com.example.fengyungo_user.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Random;

/**
 * @author 徐
 */
public class CommonUtil {
    /**
     * 格式化日期
     *
     * @param year        int
     * @param monthOfYear int
     * @param dayOfMonth  int
     * @return String
     */
    public static String formatDate(int year, int monthOfYear, int dayOfMonth) {
        return year + "-" + String.format(Locale.getDefault(), "%02d-%02d", monthOfYear, dayOfMonth);
    }

    /**
     * 格式化时间
     *
     * @param hourOfDay int
     * @param minute    int
     * @return String
     */
    public static String formatTime(int hourOfDay, int minute) {
        return String.format(Locale.getDefault(), "%02d:%02d:00", hourOfDay, minute);
    }

    /**
     * 根据当前时间生成id："yyyyMMddHHmmss"
     * @return 生成的id
     */
    public static String initIdByNowTime() {
        DateTimeFormatter formatterForId = null;
        String id = String.valueOf(new Random().nextInt());
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            formatterForId = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            LocalDateTime now = LocalDateTime.now();
            id = now.format(formatterForId);
        }
        return id;
    }

}
